package in.muton.edushare.summary.html;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.processing.objects.UnivDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class UnivSummary extends WebPage {
	public UnivSummary(IModel<Universities> univModel) {
		init(univModel);
	}

	public UnivSummary(PageParameters params) {
		int id = params.getAsInteger("0", 0);
		Universities univ = new UniversitiesDAO(EduShareSession.get()
				.getSessionORMInterface()).getUnivById(id);
		init(new CayenneModel<Universities>(univ));
	}

	private void init(IModel<Universities> univModel) {
		this.univModel = univModel;
		add(CSSPackageResource.getHeaderContribution(UnivSummary.class,
				"PrinterFriendlyCSS.css"));
		udh = new UnivDataHandler(this.univModel);
		Universities u = univModel.getObject();

		add(new Label("title", u.getUniversityName()));
		add(new Label("univName", u.getUniversityName()));
		add(new MultiLineLabel("desc", u.getUniversityDesc())
				.setEscapeModelStrings(false).setVisible(
						StringUtil.isValid(u.getUniversityDesc())));
		CayenneList<Departments> dl = udh.getAllDept();
		add(new Label("univCount", "" + dl.getObject().size()));

		add(new GridView<IModel<Departments>>("deptRows",
				new TableDateProvider<Departments>(dl)) {
			private static final long serialVersionUID = -5706939828242590271L;

			@Override
			protected void populateEmptyItem(Item<IModel<Departments>> arg0) {
				arg0.add(new Label("deptName", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Departments>> arg0) {
				arg0.add(new Label("deptName", arg0.getModelObject()
						.getObject().getDeptName()));
			}
		}.setColumns(3));

		CayenneList<Curricula> pl = udh.getPrograms();
		add(new Label("progCount", "" + pl.getObject().size()));
		add(new ListView<IModel<Curricula>>("progLV", pl) {
			private static final long serialVersionUID = 9027508432870206971L;

			@Override
			protected void populateItem(ListItem<IModel<Curricula>> arg0) {
				final Curricula c = arg0.getModelObject().getObject();
				arg0.add(new Label("progName", c.getCurriculaName()));
				arg0.add(new Label("semNum", c.getTotalSemester()
						+ " semesters"));

				CayenneList<Batches> bl = new CayenneList<Batches>(c
						.getBatches());

				arg0.add(new ListView<IModel<Batches>>("batchLV", bl
						.getObject()) {
					private static final long serialVersionUID = 1634052534273735161L;

					@Override
					protected void populateItem(ListItem<IModel<Batches>> arg0) {
						Batches b = arg0.getModelObject().getObject();
						arg0.add(new Label("batchName", b.getStartingSemester()
								.getSemesterName()
								+ ", " + b.getBatchYear()));
					}
				});
			}
		});

		CayenneList<Courses> courseL = udh.getCourses();
		add(new Label("courseCount", "" + courseL.getObject().size()));

		add(new GridView<IModel<Courses>>("courseRows",
				new TableDateProvider<Courses>(courseL)) {
			private static final long serialVersionUID = 1409560432090744962L;

			@Override
			protected void populateEmptyItem(Item<IModel<Courses>> arg0) {
				arg0.add(new Label("name", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Courses>> arg0) {
				arg0.add(new Label("name", arg0.getModelObject().getObject()
						.getCourseName()));
			}
		}.setColumns(3));

		CayenneList<Users> facList = udh.getAdmins();
		facList.add(udh.getFaculty());
		add(new Label("facCount", "" + facList.getObject().size()));
		add(new GridView<IModel<Users>>("facRows",
				new TableDateProvider<Users>(facList)) {
			private static final long serialVersionUID = 3862391235043745954L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("facName", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("facName", u.getCompleteUserName() + " ( "
						+ u.getEmail() + " )"));
			}
		}.setColumns(3));

		CayenneList<Users> stuList = udh.getStudents();
		add(new Label("stuCount", "" + stuList.getObject().size()));
		add(new GridView<IModel<Users>>("stuRows",
				new TableDateProvider<Users>(stuList)) {
			private static final long serialVersionUID = 3862391235043745954L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("name", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("name", u.getCompleteUserName() + " ( "
						+ u.getEmail() + " )"));
			}
		}.setColumns(3));
	}

	private UnivDataHandler udh;
	private IModel<Universities> univModel;
}
